package service

import (
	controller "fitness/internal/controller/query"
	"fitness/internal/dao"
	"fitness/internal/entity/system"
	"fitness/pkg/utils/R"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"strconv"
)

type RoleService struct {
	SysRoleDao *dao.SysRoleDao
	Log        *zap.Logger
}

func NewRoleService(SysRoleDao *dao.SysRoleDao, Log *zap.Logger) *RoleService {
	return &RoleService{
		SysRoleDao: SysRoleDao,
		Log:        Log,
	}
}

// GetRolesOptions 获取option
func (roleService *RoleService) GetRolesOptions(c *gin.Context) {
	var tenantQuery controller.TenantQuery
	err := c.ShouldBindQuery(&tenantQuery)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}

	options := roleService.SysRoleDao.Option(tenantQuery)
	c.JSON(http.StatusOK, R.ReturnSuccess(options))
}

func (roleService *RoleService) GetPage(c *gin.Context) {
	var tenantQuery controller.TenantPageQuery
	err := c.ShouldBindQuery(&tenantQuery)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	page, err := roleService.SysRoleDao.GetRolePage(tenantQuery)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	c.JSON(http.StatusOK, R.ReturnSuccess(page))
}

func (roleService *RoleService) GetRoleForm(c *gin.Context) {
	roleIdStr := c.Param("roleId")
	// 尝试将字符串转换为整数
	roleId, err := strconv.ParseUint(roleIdStr, 10, 64)
	if err != nil {
		// 如果转换失败，返回错误响应
		c.JSON(http.StatusOK, R.ReturnFailMsg("Invalid ID"))
		return
	}
	form, err := roleService.SysRoleDao.GetRoleForm(roleId)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	c.JSON(http.StatusOK, R.ReturnSuccess(form))
}

// 新增角色
func (roleService *RoleService) AddRole(c *gin.Context) {
	var roleForm system.RoleForm
	err := c.ShouldBindJSON(&roleForm)
	if err != nil {
		roleService.Log.Error("新增角色参数失败", zap.Error(err))
		c.JSON(http.StatusOK, R.ReturnFailMsg("新增角色参数失败"))
		return
	}
	_, err = roleService.SysRoleDao.SaveOrUpdateRole(roleForm)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	c.JSON(http.StatusOK, R.ReturnSuccess(true))
}

// 编辑角色
func (roleService *RoleService) UpdateRole(c *gin.Context) {
	var roleForm system.RoleForm
	err := c.ShouldBindJSON(&roleForm)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg("修改参数失败"))
		return
	}

	_, err = roleService.SysRoleDao.SaveOrUpdateRole(roleForm)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	c.JSON(http.StatusOK, R.ReturnSuccess(true))
}

func (roleService *RoleService) DeleteRole(c *gin.Context) {
	idStr := c.Param("ids")
	b, err := roleService.SysRoleDao.DeleteRoles(idStr)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	c.JSON(http.StatusOK, R.ReturnSuccess(b))
}

func (roleService *RoleService) GetRoleMenuIds(context *gin.Context) {
	roleIdStr := context.Param("roleId")
	b, err := roleService.SysRoleDao.GetRoleMenuIds(roleIdStr)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, R.ReturnSuccess(b))
}

func (roleService *RoleService) AssignMenusToRole(context *gin.Context) {
	roleIdStr := context.Param("roleId")
	if roleIdStr == "" {
		context.JSON(http.StatusOK, R.ReturnFailMsg("缺少角色id"))
		return
	}
	var menuIds []uint64
	err := context.ShouldBindJSON(&menuIds)
	if err != nil {
		roleService.Log.Error("分配菜单权限给角色失败", zap.Error(err))
		context.JSON(http.StatusOK, R.ReturnFailMsg("分配菜单权限给角色失败"))
		return
	}
	b, err := roleService.SysRoleDao.AssignMenusToRole(roleIdStr, menuIds)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, R.ReturnSuccess(b))
}
